#include <check.h>
#include <stdlib.h>
#include "generator/graph.h"
#include "print_graph.h"

/**
 * Checks for the basic functionality of graph_generate().
 */
START_TEST (graph_generate_check)
{
  int segments = 12;
  int players = 2;
  Graph graph;

  graph_init (&graph, segments);
  graph_generate (&graph, players, 0);

  fail_unless (segments == graph.n_segments, "Wrong number of segments.");

  print_graph (&graph);

  graph_cleanup (&graph);
}
END_TEST

/**
 * Checks that the graph output from graph_generate() produces
 * a graph where every node (except spawn and goal) has at least
 * one entry and one exit point.
 */
START_TEST (graph_connected_check)
{
  int segments = 12;
  int players = 2;
  Graph graph;

  graph_init (&graph, segments);
  graph_generate (&graph, players, 0);

  for (int i = 1; i < graph.n_segments-1; i++) {
    int from_exists = 0;

    for (int j = 0; j < i; j++) {
      int c = trimatrix_elem (graph.path, j, i);
      if (c) {
        from_exists = 1;
        break;
      }
    }

    fail_unless (from_exists, "There is no path to segment %d.", i);

    int to_exists = 0;

    for (int j = i+1; j < graph.n_segments; j++) {
      int c = trimatrix_elem (graph.path, i, j);
      if (c) {
        to_exists = 1;
        break;
      }
    }

    fail_unless (to_exists, "There is no path from segment %d.", i);
  }

  graph_cleanup (&graph);
}
END_TEST

Suite* graph_suite ()
{
  Suite* s = suite_create ("Graph");

  /* basic generate test case */
  TCase *tc_generate = tcase_create ("generate");
  tcase_add_test (tc_generate, graph_generate_check);
  suite_add_tcase (s, tc_generate);

  /* connected test case */
  TCase *tc_connected = tcase_create ("connected");
  tcase_add_test (tc_connected, graph_connected_check);
  suite_add_tcase (s, tc_connected);

  return s;
}
